package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(comma2(""))
	fmt.Println(comma2("1"))
	fmt.Println(comma2("12"))
	fmt.Println(comma2("123"))
	fmt.Println(comma2("1234"))
	fmt.Println(comma2("12345"))
	fmt.Println(comma2("123456"))
	fmt.Println(comma2("1234567"))
	fmt.Println(comma2("12345678"))
	fmt.Println(comma2("123456789"))
	fmt.Println(comma2("1234567890"))
}

func comma2(s string) string {
	// TODO 实现待优化
	var buf bytes.Buffer
	n := len(s)
	i := n % 3
	if i != 0 {
		fmt.Fprintf(&buf, s[:i])
	}
	for ; i != n; i += 3 {
		if i != 0 {
			buf.WriteByte(',')
		}
		fmt.Fprintf(&buf, s[i:i+3])
	}
	return buf.String()
}
